package hqyj.example.provider.controller;


import hqyj.example.provider.config.ServerConfig;
import hqyj.example.provider.domain.Goods;
import hqyj.example.provider.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    @Autowired
    private ServerConfig serverConfig;

    @GetMapping("all")
    public Map<String,Object> queryAllGoodsList(){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            List<Goods> goodsList = goodsService.queryAllGoodsList();
            result.put("code", 200);
            result.put("msg", "查询所有商品列表成功！");
            result.put("data", goodsList);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("code", 400);
            result.put("msg", "程序出现错误！");
            return result;
        }
    }


    @GetMapping("query/{catalog}")
    public Map<String,Object> queryAllGoodsListByCatalogName(@PathVariable("catalog")  String catalogName){
        Map<String, Object> result = new HashMap<String, Object>();
        //执行这个接口的时候，请问大家，程序如何获得当前服务的ip地址和端口号。

        try {
            List<Goods> goodsList = goodsService.queryAllGoodsListByCatalog(catalogName);
            result.put("code", 200);
            result.put("msg", "查询所有商品列表成功！");
            result.put("data", goodsList);
            result.put("host", serverConfig.getIp());
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            result.put("code", 400);
            result.put("msg", "程序出现错误！");
            return result;
        }
    }

}
